using Microsoft.AspNetCore.Mvc;
using ERPBackend.Models;
using ERPBackend.Services;
using System.Collections.Generic;
using System.Threading.Tasks;
using System;

namespace ERPBackend.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class WarehousesController : ControllerBase
    {
        private readonly IWarehouseService _warehouseService;

        public WarehousesController(IWarehouseService warehouseService)
        {
            _warehouseService = warehouseService;
        }

        // GET: api/Warehouses - 分页版本
        [HttpGet("paged")]
        public async Task<ActionResult<object>> GetWarehousesPaged(int page = 1, int pageSize = 10)
        {
            var total = await _warehouseService.GetTotalWarehousesAsync();
            var warehouses = await _warehouseService.GetWarehousesPagedAsync(page, pageSize);
            return Ok(new { items = warehouses, total });
        }

        // GET: api/Warehouses - 返回所有仓库列表
        [HttpGet]
        public async Task<ActionResult<IEnumerable<Warehouse>>> GetWarehouses()
        {
            var warehouses = await _warehouseService.GetAllWarehousesAsync();
            return Ok(warehouses);
        }

        // GET: api/Warehouses/5
        [HttpGet("{id}")]
        public async Task<ActionResult<Warehouse>> GetWarehouse(int id)
        {
            var warehouse = await _warehouseService.GetWarehouseByIdAsync(id);

            if (warehouse == null)
            {
                return NotFound();
            }

            return warehouse;
        }

        // GET: api/Warehouses/code/{warehouseCode}
        [HttpGet("code/{warehouseCode}")]
        public async Task<ActionResult<Warehouse>> GetWarehouseByCode(string warehouseCode)
        {
            var warehouse = await _warehouseService.GetWarehouseByCodeAsync(warehouseCode);

            if (warehouse == null)
            {
                return NotFound();
            }

            return warehouse;
        }

        // PUT: api/Warehouses/5
        [HttpPut("{id}")]
        public async Task<IActionResult> PutWarehouse(int id, Warehouse warehouse)
        {
            if (id != warehouse.Id)
            {
                return BadRequest();
            }

            try
            {
                var updated = await _warehouseService.UpdateWarehouseAsync(warehouse);
                if (!updated)
                {
                    return NotFound();
                }
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }

            return NoContent();
        }

        // POST: api/Warehouses
        [HttpPost]
        public async Task<ActionResult<Warehouse>> PostWarehouse(Warehouse warehouse)
        {
            try
            {
                var createdWarehouse = await _warehouseService.CreateWarehouseAsync(warehouse);
                return CreatedAtAction("GetWarehouse", new { id = createdWarehouse.Id }, createdWarehouse);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(ex.Message);
            }
        }

        // DELETE: api/Warehouses/5
        [HttpDelete("{id}")]
        public async Task<IActionResult> DeleteWarehouse(int id)
        {
            var deleted = await _warehouseService.DeleteWarehouseAsync(id);
            if (!deleted)
            {
                return NotFound();
            }

            return NoContent();
        }
    }
}